next up previous
Next: 3.2.5 Synchronization Primitives Up: 3.2 Functions Previous: 3.2.3 Off Screen Rendering

3.2.4 Rendering Contexts

To create an OpenGL rendering context call

GLXContext glXCreateContext ( Display *dpy , XVisualInfo* visual , GLXContext share _list , Bool direct ) ;

glXCreateContext returns NULL if it fails. If glXCreateContext succeeds, it returns the handle of a GLX rendering context. This handle can be used to render to both windows and GLX pixmaps.

If share _list is not NULL, then all display list indexes and definitions will be shared by share _list and the newly created rendering context. An arbitrary number of GLXContexts can share a single display list space. All sharing contexts must also share a single address space or a BadMatch error is generated.

If direct is true, then a direct rendering context will be created if the implementation supports direct rendering and the connection is to an X server that is local. If direct is False, then a rendering context that renders through the X server is created.

Direct rendering contexts may be a scarce resource in some implementations. If direct is true, and if a direct rendering context cannot be created, then glXCreateContext will attempt to create an indirect context instead.

glXCreateContext can generate the following GLX extension errors: GLXBadContext if share _list is neither zero nor a valid GLX rendering context; BadValue if visual is not a valid X Visual or if GLX does not support it; BadMatch if share _list defines an address space that cannot be shared with the newly created context or if share _list was created on a different screen than the one referenced by visual; BadAlloc if the server does not have enough resources to allocate the new context.

To determine if an OpenGL rendering context is direct call

Bool glXIsDirect ( Display *dpy , GLXContext ctx ) ;

glXIsDirect returns True if ctx is a direct rendering context, False otherwise. If ctx is not a valid GLX rendering context, a GLXBadContext error is generated.

An OpenGL rendering context is destroyed by calling

void glXDestroyContext ( Display *dpy , GLXContext ctx ) ;

If ctx is still current to any thread, ctx is not destroyed until it is no longer current. In any event, the associated XID will be destroyed and ctx cannot subsequently be made current to any thread.

glXDestroyContext will generate a GLXBadContext error if ctx is not a valid rendering context.

To copy OpenGL rendering state from one context to another, use

void glXCopyContext ( Display *dpy , GLXContext source , GLXContext dest , unsigned long mask ) ;

glXCopyContext copies selected groups of state variables from source to dest. mask indicates which groups of state variables are to be copied. mask contains the bitwise OR of the same symbolic names as described for glPushAttrib in the OpenGL Specification. The single symbolic constant GL _ALL _ATTRIB _BITS can be used to copy the maximum possible portion of the rendering state.

If source and dest do not share an address space or were not created on the same screen, a BadMatch error is generated. (Note that source and dest may be based on different X visuals and still share an address space; glXCopyContext will work correctly in such cases.) If the destination context is current for some thread then a BadAccess error is generated. If undefined mask bits are specified then a BadValue error is generated. Finally, if either source or dest is not a valid GLX rendering context, a GLXBadContext error is generated. If the source context is the same as the current context of the calling thread, and the current drawable of the calling thread is a window that is no longer valid, a GLXBadCurrentWindow is generated.

glXCopyContext performs an implicit glFlush() if source is the current context for the calling thread.

Only one rendering context may be in use, or current, for a particular thread at a given time. The minimum number of current rendering contexts that must be supported by a GLX implementation is one. (Supporting a larger number of current rendering contexts is essential for general-purpose systems, but may not be necessary for turnkey applications.)

To make a context current, call

Bool glXMakeCurrent ( Display *dpy , GLXDrawable drawable , GLXContext ctx ) ;

If the calling thread already has a current rendering context, then that context is flushed and marked as no longer current. ctx is made the current context for the calling thread.

If the drawable and ctx are not similar, a BadMatch error is generated. If ctx is current to some other thread, then glXMakeCurrent will generate a BadAccess error. GLXBadContextState is generated if there is a current rendering context and its render mode is either GL _FEEDBACK or GL _SELECT. GLXBadContextState will also be generated if glXMakeCurrent is called between a glBegin and its corresponding glEnd. If ctx is not a valid GLX rendering context, GLXBadContext is generated. If drawable is not a valid GLX drawable, a GLXBadDrawable error is generated. Finally, note that the ancillary buffers for drawable need not be allocated until a context is made current for that drawable for the first time. A BadAlloc error can be generated if the server does not have enough resources to allocate the ancillary buffers. If the previous context of the calling thread has unflushed commands, and the previous drawable is a window that is no longer valid, GLXBadCurrentWindow is generated.

To release the current context without assigning a new one, use NULL for ctx and None for drawable.

The first time ctx is made current to a GLXDrawable, its initial viewport is set. That viewport must be reset by the client when ctx is subsequently made current.

Note that when multiple threads are using their current contexts to render to the same drawable, OpenGL does not guarantee atomicity of fragment update operations. In particular, programmers may not assume that depth-buffering will automatically work correctly; there is a race condition between threads that read and update the depth buffer. Clients are responsible for avoiding this condition. They may use vendor-specific extensions or they may arrange for separate threads to draw in disjoint regions of the viewport, for example.

glXGetCurrentContext returns the current context.

GLXContext glXGetCurrentContext ( void ) ;

If there is no current context, NULL is returned. No round trip is forced to the server; unlike most X calls that return a value, glXGetCurrentContext does not flush any pending requests.

glXGetCurrentDrawable returns the XID of the current drawable.

GLXDrawable glXGetCurrentDrawable ( void ) ;

If there is no current drawable, None is returned. No round trip is forced to the server; unlike most X calls that return a value, glXGetCurrentDrawable does not flush any pending requests.



next up previous
Next: 3.2.5 Synchronization Primitives Up: 3.2 Functions Previous: 3.2.3 Off Screen Rendering



Mark Segal
Wed Jan 11 18:38:15 PST 1995